<?php
class ControllerModuleTDgiftonorder extends Controller {
	public function index() {
		$this->language->load('module/td_giftonorder');
		$this->document->setTitle($this->language->get('module_title'));
		$this->load->model('module/td_giftonorder');
		$this->model_module_td_giftonorder->tdGiftInstall();
		$this->data['heading_title'] = $this->language->get('module_title');

		$this->data['button_add'] = $this->language->get('button_add');
		$this->data['add_action'] = $this->url->link('module/td_giftonorder/getCreate', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['action_delete'] = $this->url->link('module/td_giftonorder/postDelete', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['button_delete'] = $this->language->get('button_delete');
		$this->data['entry_id'] = $this->language->get('entry_id');
		$this->data['entry_name'] = $this->language->get('gift_name_text');
		$this->data['entry_made_for'] = $this->language->get('this_gift_made_for_text');
		$this->data['entry_date_start'] = $this->language->get('date_start_text');
		$this->data['entry_date_end'] = $this->language->get('date_end_text');
		$this->data['entry_edit'] = $this->language->get('entry_edit');
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		if (isset($this->session->data['error_permission'])) {
			$this->data['error_permission'] = $this->session->data['error_permission'];

			unset($this->session->data['error_permission']);
		} else {
			$this->data['error_permission'] = '';
		}
		if (isset($this->session->data['error_giftcheck'])) {
			$this->data['error_giftcheck'] = $this->session->data['error_giftcheck'];

			unset($this->session->data['error_giftcheck']);
		} else {
			$this->data['error_giftcheck'] = '';
		}
		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_module'),
			'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);
		$this->data['gifts'] = array();
		$this->data['gifts'] = $this->model_module_td_giftonorder->getGifts();
		$this->document->addStyle('view/stylesheet/giftonorder/style.css');
		$this->template = 'module/giftonorder/td_giftonorder.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'module/td_giftonorder'))
			return false;
		else
			return true;
	}
	public function getCreate(){
		$this->language->load('module/td_giftonorder');
		$this->document->setTitle($this->language->get('gift_program_text'));
		$this->data['heading_title'] = $this->language->get('gift_program_text');
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['save_url'] = $this->url->link('module/td_giftonorder/postCreate', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['cancel_url'] = $this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL');
		//Text
		$this->data['gift_name_text'] = $this->language->get('gift_name_text');
		$this->data['gift_category_text'] = $this->language->get('gift_category_text');
		$this->data['this_gift_made_for_text'] = $this->language->get('this_gift_made_for_text');
		$this->data['customers_text'] = $this->language->get('customers_text');
		$this->data['orders_text'] = $this->language->get('orders_text');
		$this->data['products_text'] = $this->language->get('products_text');
		$this->data['minimum_cart_amount_text'] = $this->language->get('minimum_cart_amount_text');
		$this->data['products_gift_offered'] = $this->language->get('products_gift_offered');
		$this->data['date_start_text'] = $this->language->get('date_start_text');
		$this->data['date_end_text'] = $this->language->get('date_end_text');
		$this->data['mesage_text'] = $this->language->get('mesage_text');
		$this->data['option_name_text'] = $this->language->get('option_name_text');
		$this->data['option_value_text'] = $this->language->get('option_value_text');
		$this->data['error_warning'] = '';
		$this->data['token'] = $this->request->get['token'];
		//Breadcrumbs
		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_module'),
			'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);
		//customers group
		$this->load->model('sale/customer_group');
		$this->data['customers_group'] = $this->model_sale_customer_group->getCustomerGroups();
		//categories
		$this->load->model('catalog/category');
		$categories = $this->model_catalog_category->getCategories(0);
		foreach ($categories as $category) {
			$children_data = array();

			$children = $this->model_catalog_category->getCategories($category['category_id']);

			foreach ($children as $child) {
				$children_data[] = array(
					'category_id' => $child['category_id'],
					'name'        => $child['name'],
				);
			}

			$this->data['categories'][] = array(
				'category_id' => $category['category_id'],
				'name'        => $category['name'] ,
				'children'    => $children_data,
			);
		}
		$this->document->addStyle('view/stylesheet/giftonorder/style.css');
		$this->template = 'module/giftonorder/td_giftonorder_getCreate.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}
	public function postCreate(){
		if($this->request->server['REQUEST_METHOD']!= 'POST')
		$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
		$this->language->load('module/td_giftonorder');
		if (!$this->validate()) {
			$this->session->data['error_permission'] = $this->language->get('error_permission');
			$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
		}
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->load->model('module/td_giftonorder');
			$data = array();
			$data['name'] = $this->request->post['name'];
			$data['options'] = $this->request->post['options'];
			$data['date_start'] = $this->request->post['date_start'];
			$data['date_end'] = $this->request->post['date_end'];
			$this->model_module_td_giftonorder->addGift($data);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
		}
	}
	public function getEdit(){
		$this->language->load('module/td_giftonorder');
		$this->document->setTitle($this->language->get('module_title'));
		$this->data['heading_title'] = $this->language->get('module_title');
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['save_url'] = $this->url->link('module/td_giftonorder/postEdit', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['cancel_url'] = $this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL');
		//Text
		$this->data['gift_name_text'] = $this->language->get('gift_name_text');
		$this->data['gift_category_text'] = $this->language->get('gift_category_text');
		$this->data['this_gift_made_for_text'] = $this->language->get('this_gift_made_for_text');
		$this->data['customers_text'] = $this->language->get('customers_text');
		$this->data['orders_text'] = $this->language->get('orders_text');
		$this->data['products_text'] = $this->language->get('products_text');
		$this->data['minimum_cart_amount_text'] = $this->language->get('minimum_cart_amount_text');
		$this->data['products_gift_offered'] = $this->language->get('products_gift_offered');
		$this->data['date_start_text'] = $this->language->get('date_start_text');
		$this->data['date_end_text'] = $this->language->get('date_end_text');
		$this->data['error_warning'] = '';
		$this->data['token'] = $this->request->get['token'];
		$this->data['mesage_text'] = $this->language->get('mesage_text');
		$this->data['option_name_text'] = $this->language->get('option_name_text');
		$this->data['option_value_text'] = $this->language->get('option_value_text');
		//Breadcrumbs
		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_module'),
			'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);
		//customers group
		$this->load->model('sale/customer_group');
		$this->data['customers_group'] = $this->model_sale_customer_group->getCustomerGroups();
		//categories
		$this->load->model('catalog/category');
		$categories = $this->model_catalog_category->getCategories(0);
		foreach ($categories as $category) {
			$children_data = array();

			$children = $this->model_catalog_category->getCategories($category['category_id']);

			foreach ($children as $child) {
				$children_data[] = array(
					'category_id' => $child['category_id'],
					'name'        => $child['name'],
				);
			}

			$this->data['categories'][] = array(
				'category_id' => $category['category_id'],
				'name'        => $category['name'] ,
				'children'    => $children_data,
			);
		}
		//Gift data
		$this->load->model('module/td_giftonorder');
		$this->data['gift'] = $this->model_module_td_giftonorder->getGift($this->request->get['gift_id']);
		
		$this->data['options'] = unserialize(base64_decode($this->data['gift']['options']));
		if(isset($this->data['options']['products'])){
			$products = $this->data['options']['products'];
			$this->load->model('catalog/product');
			$this->data['products'] = array();
			foreach($products as $key=> $id){
				$this->data['products'][$key]['product_id'] = $id;
				$product_info = $this->model_catalog_product->getProduct($id);
				$this->data['products'][$key]['name'] = $product_info['name'];
			}
		}
		else
			$this->data['products'] = array();
		$this->document->addStyle('view/stylesheet/giftonorder/style.css');	
		$this->template = 'module/giftonorder/td_giftonorder_getEdit.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}
	public function postEdit(){
		if($this->request->server['REQUEST_METHOD']!= 'POST')
		$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
		$this->language->load('module/td_giftonorder');
		if (!$this->validate()) {
			$this->session->data['error_permission'] = $this->language->get('error_permission');
			$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
		}
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->load->model('module/td_giftonorder');
			$data = array();
			$data['id'] = $this->request->post['id'];
			$data['name'] = $this->request->post['name'];
			$data['options'] = $this->request->post['options'];
			$data['date_start'] = $this->request->post['date_start'];
			$data['date_end'] = $this->request->post['date_end'];
			$this->model_module_td_giftonorder->editGift($data);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
		}
	}
	public function postDelete(){
		if($this->request->server['REQUEST_METHOD']!= 'POST')
		$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
		$this->language->load('module/td_giftonorder');
		if (!$this->validate()) {
			$this->session->data['error_permission'] = $this->language->get('error_permission');
			$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
		}
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			if(!isset($this->request->post['delete'])){
				$this->session->data['error_giftcheck'] = $this->language->get('error_giftcheck');
				$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
			}
			else{
				$this->load->model('module/td_giftonorder');
				foreach($this->request->post['delete'] as $id)
					$this->model_module_td_giftonorder->deleteGift($id);
				$this->session->data['success'] = $this->language->get('text_success');
				$this->redirect($this->url->link('module/td_giftonorder', 'token=' . $this->session->data['token'], 'SSL'));
			}
		}
	}
}
?>